package controller;

import Utils.ConstantUtils;
import Utils.ResponseData;
import com.baomidou.kaptcha.Kaptcha;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.MediaType;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

/**
 * @Description:
 * @Author: liujh
 * @Date:
 * @UpdateUser: liujh
 * @Date: 2020/09/15$ 09:56$
 * @UpdateRemark:
 * @Version: 1.0
 */
@Controller
@RequestMapping(value = "kaptcha")
@Slf4j
public class KaptchaController {

    @Value("${custom-site.kaptcha.expire-time}")
    private Long expireTime;

    @Autowired
    private Kaptcha kaptcha;

    @GetMapping(value = "render")
    @ResponseBody
    public void render() {
        String code = kaptcha.render();
        log.info("kaptcha:{}",code);
    }

    @PostMapping("valid")
    @ResponseBody
    public void validDefaultTime(@RequestParam String code) {
        //default timeout 900 seconds
        kaptcha.validate(code);
    }

    @PostMapping("validTime")
    @ResponseBody
    public void validWithTime(@RequestParam String code) {
        kaptcha.validate(code, expireTime);
    }
}
